//
//  HZL_MapViewController.m
//  B_Project
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_MapViewController.h"
#import <MapKit/MapKit.h>
#import "AMapNaviKit.h"
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) //用来获取手机的系统，判断系统是多少

@interface HZL_MapViewController ()<MKMapViewDelegate, AMapNaviManagerDelegate, AMapNaviViewControllerDelegate>
@property (nonatomic, strong) MKMapView *map;
@property (nonatomic, strong) CLLocationManager  *CLLocationManager;
@end
@implementation HZL_MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"导航" style:UIBarButtonItemStylePlain target:self action:@selector(navigation)];

    self.map = [[MKMapView alloc]initWithFrame:self.view.frame];
    self.map.mapType = MKMapTypeStandard;
    [self.view addSubview:self.map];
    
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(self.latitude.doubleValue, self.longitude.doubleValue);
    float dis = 0.05;
    MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(dis, dis));
    [self.map setRegion:[_map regionThatFits:region] animated:YES];
    
    // 大头针
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(self.latitude.doubleValue, self.longitude.doubleValue);

    
   
    if (self.address) {
        annotation.title = self.address[@"name"];
        annotation.subtitle = self.address[@"address"];
        
    }else {
        annotation.subtitle = self.add;
        annotation.title = self.name;
    }

    [self.map addAnnotation:annotation];
}

#pragma mark - 导航
- (void)navigation
{
    CLLocationCoordinate2D startCoor = self.map.userLocation.location.coordinate;
    CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(self.latitude.doubleValue, self.longitude.doubleValue);
    
    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6以下，调用google map
        
        NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];
    
        urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *aURL = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:aURL];
    } else { // 直接调用ios自己带的apple map
        
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];
        if (self.address) {
            toLocation.name = self.address[@"name"];
        }if (self.add) {
            toLocation.name = self.name;
        }
        
        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
